package com.yanggeng.demo.beginnerguidedemo.widget;

import android.app.Activity;
import android.app.Dialog;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.yanggeng.demo.beginnerguidedemo.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 可缩放大图查看器
 * @author aiden@tronsis.com
 * @date 2018/6/8 10:17
 */
public class ScaleImageView {

    private Activity mActivity;
    private List<String> mUrls;
    private List<File> mFiles;
    private List<View> mViews;
    private Dialog dialog;
    private ViewPager mViewPager;
    private int mSelectedPosition = 0;

    public ScaleImageView(Activity activity) {
        this.mActivity = activity;
        init();
    }

    private void init() {
        View view = mActivity.getLayoutInflater().inflate(R.layout.dialog_scale_image, null);
        mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
        dialog = new Dialog(mActivity, R.style.FullScreenDialog);
        dialog.setContentView(view);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mSelectedPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void create() {
        dialog.show();
        mViews = new ArrayList<>();

        for (File file : mFiles) {
            Log.e("ScaleImageView", file.getAbsolutePath());
            View view = mActivity.getLayoutInflater().inflate(R.layout.layout_scale_image_view, null);
            SubsamplingScaleImageView imageView = (SubsamplingScaleImageView) view.findViewById(R.id.scale_image_view);
            mViews.add(view);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            // 加载大图片时，可设置预览时加载小图
            // imageView.setImage(imageSource, previewSource);
            imageView.setImage(ImageSource.uri(Uri.fromFile(file)));
        }
        ScaleImagePageAdapter adapter = new ScaleImagePageAdapter(mViews, dialog);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(mSelectedPosition);
    }

    public void setFiles(List<File> files, int selectedPosition) {
        if (files == null || files.size() <= 0) {
            throw new RuntimeException("Files cannot be null or empty!");
        }
        if (mFiles == null) {
            mFiles = new ArrayList<>();
        } else {
            mFiles.clear();
        }
        for (File file : files) {
            Log.e("ScaleImageView", file.getAbsolutePath());
        }
        mFiles.addAll(files);
        mSelectedPosition = selectedPosition;
    }

    private class ScaleImagePageAdapter extends PagerAdapter {

        private List<View> views;
        private Dialog dialog;

        public ScaleImagePageAdapter(List<View> views, Dialog dialog) {
            this.views = views;
            this.dialog = dialog;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            if (position == 0 && views.size() == 0) {
                dialog.dismiss();
                return;
            }
            if (position == views.size()) {
                container.removeView(views.get(--position));
            } else {
                container.removeView(views.get(position));
            }
        }
//
//        @Override
//        public int getItemPosition(@NonNull Object object) {
//            return super.getItemPosition(object);
//        }
    }
}
